/**
 * Created by 宏宾 on 2017/3/12.
 */


var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid"
        },
        key:{
            url:"xUrl"
        }
    },

    check: {
        enable: true,
        chkboxType: {"Y":"", "N":""}
    },
    view: {
        dblClickExpand: false
    },
    callback:{
        beforeClick: beforeClick,
        onCheck: onCheck

    }
};


function beforeClick(treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("treeRole");
    zTree.checkNode(treeNode, !treeNode.checked, null, true);
    return false;
}

function onCheck(e, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("treeRole"),
        nodes = zTree.getCheckedNodes(true),
        v = "",ids="";
    for (var i=0, l=nodes.length; i<l; i++) {
        v += nodes[i].name + ",";
        ids += nodes[i].id + ",";
    }
    if (v.length > 0 ) v = v.substring(0, v.length-1);
    if (ids.length > 0 ) ids = ids.substring(0, ids.length-1);
    var cityObj = $("#permissionList");
    cityObj.attr("value", v);
    $("#pids").attr("value",ids);
}



function showMenu() {

    //获取父元素的X轴偏离
    var layer_page = $(".layui-layer-page").offset();

    var cityObj = $("#permissionList");
    var cityOffset = $("#permissionList").offset();
    var X = cityOffset.left - layer_page.left;
    var Y =  cityOffset.top - layer_page.top-14;

    $("#menuContent").css({left:X+ "px", top:Y  + "px"}).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "permissionList" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
        hideMenu();
    }
}










var zTree;
$(function () {

    $.get("/permission/list",function (data) {
        var  zNodes = data.data;

        var  treeObj = $.fn.zTree.init($("#treeRole"), setting, zNodes);
        treeObj.expandAll(true);

        zTree = $.fn.zTree.getZTreeObj("treeRole");

        //设置父亲节点不能被选中
        var nodes=treeObj.getNodes();

        nocheck(nodes);
        var p_ids = $("#p_ids").val();//当前权限的ids

        checknodes(p_ids);

    });

});
function nocheck(nodes) {
    if(nodes){
        for(var i=0;i<nodes.length;i++){

            if(nodes[i].isParent == true){//如果是父节点，不让选中
                //进行下一步孩子的循环
                nodes[i].nocheck=true;
                console.info(nodes[i]);
                var nodeChildrens=nodes[i].children;
                nocheck(nodeChildrens);
            }

        }


    }
}

function checknodes(ids) {
    var zTree = $.fn.zTree.getZTreeObj("treeRole");

     if(ids){
         var s =new Array();
         s = ids.split(",");

         for(var i=0;i<s.length;i++){
             var node = zTree.getNodeByParam("id",s[i]);

             node.checked =true;
         }
     }
}
